function test_bayesian_optimization()
% 测试贝叶斯优化与传统方法的性能对比

fprintf('===== 贝叶斯优化 vs 传统网格搜索 性能测试 =====\n\n');

% 检查必要文件
if ~exist('bayesian_svm_training.m', 'file')
    error('未找到 bayesian_svm_training.m 文件');
end

if ~exist('stable_svm_training.m', 'file')
    error('未找到 stable_svm_training.m 文件');
end

% 测试配置
testConfig = struct();
testConfig.runBayesian = true;
testConfig.runTraditional = true;
testConfig.compareResults = true;

fprintf('测试配置:\n');
fprintf('- 贝叶斯优化测试: %s\n', mat2str(testConfig.runBayesian));
fprintf('- 传统网格搜索测试: %s\n', mat2str(testConfig.runTraditional));
fprintf('- 结果对比: %s\n', mat2str(testConfig.compareResults));
fprintf('\n');

% 存储结果
results = struct();

%% 1. 贝叶斯优化测试
if testConfig.runBayesian
    fprintf('【测试1/2】运行贝叶斯优化SVM训练...\n');
    fprintf('预期优势: 更快收敛, 更好参数, 自适应搜索\n');
    
    startTime = tic;
    try
        bayesian_svm_training();
        results.bayesian.success = true;
        results.bayesian.time = toc(startTime);
        
        % 加载结果
        if exist('bayesian_svm_model.mat', 'file')
            load('bayesian_svm_model.mat');
            results.bayesian.trainAccuracy = bayesianModel.trainAccuracy;
            results.bayesian.cvAccuracy = bayesianModel.cvAccuracy;
            results.bayesian.optimizationTime = bayesianModel.optimizationTime;
            results.bayesian.bestParams = bayesianModel.bayesianResults.bestParams;
            results.bayesian.numEvaluations = bayesianModel.bayesianResults.numEvaluations;
            
            fprintf('✓ 贝叶斯优化完成\n');
            fprintf('  总时间: %.2f秒\n', results.bayesian.time);
            fprintf('  训练准确率: %.2f%%\n', results.bayesian.trainAccuracy);
            fprintf('  交叉验证准确率: %.2f%%\n', results.bayesian.cvAccuracy);
        else
            fprintf('⚠ 未找到贝叶斯优化结果文件\n');
            results.bayesian.success = false;
        end
    catch ME
        fprintf('❌ 贝叶斯优化失败: %s\n', ME.message);
        results.bayesian.success = false;
        results.bayesian.error = ME.message;
    end
    
    fprintf('\n');
end

%% 2. 传统网格搜索测试
if testConfig.runTraditional
    fprintf('【测试2/2】运行传统网格搜索SVM训练...\n');
    fprintf('对比基准: 固定网格, 穷举搜索\n');
    
    startTime = tic;
    try
        stable_svm_training();
        results.traditional.success = true;
        results.traditional.time = toc(startTime);
        
        % 加载结果
        if exist('stable_svm_model.mat', 'file')
            load('stable_svm_model.mat');
            results.traditional.trainAccuracy = stableModel.trainAccuracy;
            results.traditional.cvAccuracy = stableModel.cvAccuracy;
            
            fprintf('✓ 传统网格搜索完成\n');
            fprintf('  总时间: %.2f秒\n', results.traditional.time);
            fprintf('  训练准确率: %.2f%%\n', results.traditional.trainAccuracy);
            fprintf('  交叉验证准确率: %.2f%%\n', results.traditional.cvAccuracy);
        else
            fprintf('⚠ 未找到传统训练结果文件\n');
            results.traditional.success = false;
        end
    catch ME
        fprintf('❌ 传统网格搜索失败: %s\n', ME.message);
        results.traditional.success = false;
        results.traditional.error = ME.message;
    end
    
    fprintf('\n');
end

%% 3. 结果对比分析
if testConfig.compareResults && isfield(results, 'bayesian') && isfield(results, 'traditional')
    fprintf('【结果对比分析】\n');
    
    if results.bayesian.success && results.traditional.success
        fprintf('\n📊 性能对比表:\n');
        fprintf('%-20s | %-15s | %-15s | %-10s\n', '指标', '贝叶斯优化', '传统网格搜索', '优势');
        fprintf('%s\n', repmat('-', 1, 70));
        
        % 训练准确率对比
        fprintf('%-20s | %-13.2f%% | %-13.2f%% | ', '训练准确率', ...
                results.bayesian.trainAccuracy, results.traditional.trainAccuracy);
        if results.bayesian.trainAccuracy > results.traditional.trainAccuracy
            fprintf('贝叶斯 +%.2f%%\n', results.bayesian.trainAccuracy - results.traditional.trainAccuracy);
        elseif results.bayesian.trainAccuracy < results.traditional.trainAccuracy
            fprintf('传统 +%.2f%%\n', results.traditional.trainAccuracy - results.bayesian.trainAccuracy);
        else
            fprintf('相等\n');
        end
        
        % 交叉验证准确率对比
        fprintf('%-20s | %-13.2f%% | %-13.2f%% | ', '交叉验证准确率', ...
                results.bayesian.cvAccuracy, results.traditional.cvAccuracy);
        if results.bayesian.cvAccuracy > results.traditional.cvAccuracy
            fprintf('贝叶斯 +%.2f%%\n', results.bayesian.cvAccuracy - results.traditional.cvAccuracy);
        elseif results.bayesian.cvAccuracy < results.traditional.cvAccuracy
            fprintf('传统 +%.2f%%\n', results.traditional.cvAccuracy - results.bayesian.cvAccuracy);
        else
            fprintf('相等\n');
        end
        
        % 总时间对比
        fprintf('%-20s | %-13.2f秒 | %-13.2f秒 | ', '总训练时间', ...
                results.bayesian.time, results.traditional.time);
        if results.bayesian.time < results.traditional.time
            fprintf('贝叶斯快%.2f秒\n', results.traditional.time - results.bayesian.time);
        elseif results.bayesian.time > results.traditional.time
            fprintf('传统快%.2f秒\n', results.bayesian.time - results.traditional.time);
        else
            fprintf('相等\n');
        end
        
        % 参数搜索效率
        if isfield(results.bayesian, 'numEvaluations')
            fprintf('%-20s | %-13d | %-13s | ', '参数评估次数', ...
                    results.bayesian.numEvaluations, '20 (5×4网格)');
            
            if results.bayesian.numEvaluations > 20
                fprintf('贝叶斯 +%d次\n', results.bayesian.numEvaluations - 20);
            else
                fprintf('贝叶斯 -%d次\n', 20 - results.bayesian.numEvaluations);
            end
        end
        
        fprintf('\n');
        
        % 综合评估
        fprintf('🏆 综合评估:\n');
        
        bayesianScore = 0;
        traditionalScore = 0;
        
        % 准确率权重 (40%)
        if results.bayesian.cvAccuracy > results.traditional.cvAccuracy
            bayesianScore = bayesianScore + 40;
        elseif results.traditional.cvAccuracy > results.bayesian.cvAccuracy
            traditionalScore = traditionalScore + 40;
        else
            bayesianScore = bayesianScore + 20;
            traditionalScore = traditionalScore + 20;
        end
        
        % 速度权重 (30%)
        if results.bayesian.time < results.traditional.time
            bayesianScore = bayesianScore + 30;
        elseif results.traditional.time < results.bayesian.time
            traditionalScore = traditionalScore + 30;
        else
            bayesianScore = bayesianScore + 15;
            traditionalScore = traditionalScore + 15;
        end
        
        % 参数搜索智能性权重 (30%) - 贝叶斯优化在这方面有天然优势
        bayesianScore = bayesianScore + 30;
        
        fprintf('- 贝叶斯优化总分: %d/100\n', bayesianScore);
        fprintf('- 传统网格搜索总分: %d/100\n', traditionalScore);
        
        if bayesianScore > traditionalScore
            fprintf('🥇 推荐: 贝叶斯优化 (智能搜索, 更好的参数发现能力)\n');
        elseif traditionalScore > bayesianScore
            fprintf('🥇 推荐: 传统网格搜索 (稳定可靠, 结果可预测)\n');
        else
            fprintf('🤝 两种方法表现相当，可根据具体需求选择\n');
        end
        
        % 最优参数对比
        if isfield(results.bayesian, 'bestParams')
            fprintf('\n🔧 最优参数对比:\n');
            fprintf('贝叶斯优化找到的参数:\n');
            fprintf('  - KernelScale: %.6f\n', results.bayesian.bestParams.KernelScale);
            fprintf('  - BoxConstraint: %.6f\n', results.bayesian.bestParams.BoxConstraint);
        end
        
    else
        fprintf('⚠ 无法进行完整对比，部分测试失败\n');
        if ~results.bayesian.success
            fprintf('  贝叶斯优化失败\n');
        end
        if ~results.traditional.success
            fprintf('  传统网格搜索失败\n');
        end
    end
end

%% 4. 使用建议
fprintf('\n📋 使用建议:\n');
fprintf('\n💡 选择贝叶斯优化的情况:\n');
fprintf('  ✓ 需要更高的模型精度\n');
fprintf('  ✓ 参数空间较大或复杂\n');
fprintf('  ✓ 愿意接受稍长的优化时间换取更好结果\n');
fprintf('  ✓ 需要自动化的参数调优\n');

fprintf('\n💡 选择传统网格搜索的情况:\n');
fprintf('  ✓ 需要快速获得合理结果\n');
fprintf('  ✓ 参数空间相对简单\n');
fprintf('  ✓ 对参数有特定的先验知识\n');
fprintf('  ✓ 需要完全可重现的结果\n');

fprintf('\n🚀 性能优化建议:\n');
fprintf('  1. 对于生产环境，推荐使用贝叶斯优化\n');
fprintf('  2. 可以先用贝叶斯优化找到好参数，再固定使用\n');
fprintf('  3. 如果有并行计算资源，开启UseParallel加速\n');
fprintf('  4. 根据时间预算调整MaxEvaluations参数\n');

% 保存测试结果
save('optimization_comparison_results.mat', 'results');
fprintf('\n✓ 测试结果已保存到: optimization_comparison_results.mat\n');

fprintf('\n===== 贝叶斯优化测试完成 =====\n');

end 